package com.transer.web.validator;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
import com.transer.persistance.dao.IUserDao;
import com.transer.persistance.exception.TranserDatabaseException;
import com.transer.utils.Utils;
import com.transer.web.action.signup.RegisteAction;

public class EmailExsitValidator extends FieldValidatorSupport {

	public void validate(Object object) throws ValidationException {
		String fieldName = getFieldName();
		Object value = getFieldValue(fieldName, object);

		IUserDao userDao = null;

		if (object instanceof RegisteAction) {
			RegisteAction action = (RegisteAction) object;
			userDao = action.getUserDao();
		}

		if (userDao != null) {
			boolean exsit = false;
			try {
				exsit = userDao.isEmailExsit(Utils.getString(value)) != null;
			} catch (TranserDatabaseException e) {
				exsit = false;
			}
			if (exsit) {
				addFieldError(fieldName, object);
			}
		}
	}
}